﻿#建议保存编码为：bom头 + utf8
param
(
	[parameter(Mandatory = $true)]
	[ValidateNotNullOrEmpty()]
	[Alias("cname")][String]$cluster名,

	[parameter(Mandatory = $true)]
	[ValidateNotNullOrEmpty()]
	[String]$ip
)

& "${PSScriptRoot}/_bak_eds.ps1"  $cluster名
& "${PSScriptRoot}/check_cds.ps1" $cluster名
if ($LASTEXITCODE -eq 0)
{
	Set-Location "${PSScriptRoot}/${cluster名}"
}
else
{
	& "${PSScriptRoot}/Write-Error2.ps1"  '错误：没有这个cluster。退出码1'
	exit 1
}

$private:有endpoint文件 = & "${PSScriptRoot}/check_eds.ps1" $ip
if ( $private:有endpoint文件 -eq $true)
{
	Remove-Item -Force -LiteralPath "${PSScriptRoot}/eds/${ip}"
	$private:eds = Get-Content -LiteralPath "${PSScriptRoot}/${cluster名}/eds.json" | ConvertFrom-Json -Depth 99
	$temp002 = @()
	foreach ($temp001 in $private:eds.resources[0].endpoints[0].lb_endpoints)
	{
		if ($temp001.endpoint.address.socket_address.address -eq ${ip})
		{
			continue
		}
		else
		{
			$temp002 += $temp001
		}
	}
	$private:eds.resources[0].endpoints[0].lb_endpoints = $temp002
	$private:eds2 = $private:eds | ConvertTo-Json -Depth 99
	Set-Content -LiteralPath "${PSScriptRoot}/${cluster名}/eds.json" -Value $private:eds2 -Encoding utf8NoBOM
}
else
{
	& "${PSScriptRoot}/Write-Error2.ps1"  '错误：没有有这个endpoint,或endpoint名字不合法。退出码2'
	Set-Location ..
	exit 2
}

& "${PSScriptRoot}/_flashfile.ps1" "${PSScriptRoot}/${cluster名}/eds.json"
& "${PSScriptRoot}/Write-Host2.ps1"  -ForegroundColor Green '信息：endpoint删除成功'
Set-Location ..
exit 0
